package org.phalanx.client.dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.phalanx.client.UI.PhalanxIconButton;

public class MainMenuPanel extends JPanel {
	private PhalanxWindow masterFrame;
	private JPanel menuFrame;
	private JPanel statusFrame;
	private ArrayList<Component> leftMenu;
	private ArrayList<Component> rightMenu;
	private PhalanxIconButton connectToServer;
	private PhalanxIconButton joinServerNode;
	private PhalanxIconButton mapEditorButton;
	private PhalanxIconButton helpButton;
	private PhalanxIconButton preferencesButton;
	private PhalanxIconButton exitButton;
	
	public static final String NAME = "MAINMENUPANEL";
	
	public MainMenuPanel(PhalanxWindow masterFrame){
		this.masterFrame = masterFrame;
		initializePanel();
	}
	
	private void initializePanel(){
		statusFrame = new PhalanxOnlineStatusPanel(masterFrame);
		menuFrame = new JPanel();
		menuFrame.setBackground(Color.black);
		menuFrame.setLayout(new BoxLayout(menuFrame,BoxLayout.Y_AXIS));
		
		leftMenu = new ArrayList<Component>();
		rightMenu = new ArrayList<Component>();
		populateMenuButtons();
		
		this.setBackground(Color.black);
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		for (int i = 0; i < leftMenu.size(); i++){
			menuFrame.add(createMenuPanels(i));
		}
		
		this.add(statusFrame);
		this.add(menuFrame);
	}
	
	private void populateMenuButtons(){
		createMenuButtons();
		leftMenu = new ArrayList<Component>();
		rightMenu = new ArrayList<Component>();
		
		leftMenu.add(preferencesButton);
		leftMenu.add(mapEditorButton);
		leftMenu.add(helpButton);
		
		rightMenu.add(connectToServer);
		rightMenu.add(joinServerNode);
		rightMenu.add(exitButton);
	}
	
	private void createMenuButtons(){
		connectToServer = new PhalanxIconButton();
		connectToServer.setForeground(Color.white);
		connectToServer.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Connect.png"));
		connectToServer.setHoveredIcon(new ImageIcon("./resources/images/menuItems/ConnectHover.png"));
		connectToServer.setClickedIcon(connectToServer.getIcon(PhalanxIconButton.DEFAULT_ICON));
		connectToServer.setText("Connect To Network");
		
		joinServerNode = new PhalanxIconButton();
		joinServerNode.setForeground(Color.white);
		joinServerNode.setDefaultIcon(new ImageIcon("./resources/images/menuItems/ServerNode.png"));
		joinServerNode.setHoveredIcon(new ImageIcon("./resources/images/menuItems/ServerNodeHover.png"));
		joinServerNode.setClickedIcon(joinServerNode.getIcon(PhalanxIconButton.DEFAULT_ICON));
		joinServerNode.setText("Join Game Node");
		joinServerNode.setEnabled(false);
		
		preferencesButton = new PhalanxIconButton();
		preferencesButton.setForeground(Color.white);
		preferencesButton.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Preferences.png"));
		preferencesButton.setHoveredIcon(new ImageIcon("./resources/images/menuItems/PreferencesHover.png"));
		preferencesButton.setClickedIcon(preferencesButton.getIcon(PhalanxIconButton.DEFAULT_ICON));
		preferencesButton.setText("Preferences");
		
		mapEditorButton = new PhalanxIconButton();
		mapEditorButton.setForeground(Color.white);
		mapEditorButton.setDefaultIcon(new ImageIcon("./resources/images/menuItems/MapEditor.png"));
		mapEditorButton.setHoveredIcon(new ImageIcon("./resources/images/menuItems/MapEditorHover.png"));
		mapEditorButton.setClickedIcon(mapEditorButton.getIcon(PhalanxIconButton.DEFAULT_ICON));
		mapEditorButton.setText("Map Editor");
		
		helpButton = new PhalanxIconButton();
		helpButton.setForeground(Color.white);
		helpButton.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Help.png"));
		helpButton.setHoveredIcon(new ImageIcon("./resources/images/menuItems/HelpHover.png"));
		helpButton.setClickedIcon(helpButton.getIcon(PhalanxIconButton.DEFAULT_ICON));
		helpButton.setText("Help");
		
		exitButton = new PhalanxIconButton();
		exitButton.setForeground(Color.white);
		exitButton.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Exit.png"));
		exitButton.setHoveredIcon(new ImageIcon("./resources/images/menuItems/ExitHover.png"));
		exitButton.setClickedIcon(exitButton.getIcon(PhalanxIconButton.DEFAULT_ICON));
		exitButton.setText("Exit Phalanx");
		
		exitButton.addButtonListener(masterFrame);
		connectToServer.addButtonListener(masterFrame);
		joinServerNode.addButtonListener(masterFrame);
		mapEditorButton.addButtonListener(masterFrame);
	}
	
	private JPanel createMenuPanels(int line){
		JPanel menuLine1 = createJPanel();
		menuLine1.setLayout(new BoxLayout(menuLine1, BoxLayout.X_AXIS));
		menuLine1.setBackground(Color.black);
		
		JPanel menu1Left = createJPanel();
		menu1Left.setLayout(new FlowLayout(FlowLayout.CENTER));
		menu1Left.setBackground(Color.black);
		menu1Left.add(leftMenu.get(line));
		
		JPanel menu1Right = createJPanel();
		menu1Right.setLayout(new FlowLayout(FlowLayout.CENTER));
		menu1Right.setBackground(Color.black);
		menu1Right.add(rightMenu.get(line));

		menuLine1.add(menu1Left);
		menuLine1.add(menu1Right);
		
		return menuLine1;
	}
	
	private JPanel createJPanel(){
		JPanel temp = new JPanel();
		
		temp.setBackground(Color.black);
		
		return temp;
	}
	
	public PhalanxIconButton getConnectToServer() {
		return connectToServer;
	}

	public PhalanxIconButton getExitButton() {
		return exitButton;
	}
	
	public PhalanxIconButton getServerNodeButton(){
		return this.joinServerNode;
	}
	
	public PhalanxIconButton getHelpButton(){
		return this.helpButton;
	}
	
	public PhalanxIconButton getMapEditorButton(){
		return this.mapEditorButton;
	}
	
	public void setConnectionStateButton(boolean isOnline){
		if (isOnline){
			connectToServer.setText("Disconnect From Network");
			connectToServer.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Disconnect.png"));
			connectToServer.setHoveredIcon(new ImageIcon("./resources/images/menuItems/DisconnectHover.png"));
			connectToServer.setClickedIcon(connectToServer.getIcon(PhalanxIconButton.DEFAULT_ICON));
		} else{
			connectToServer.setText("Connect To Network");
			connectToServer.setDefaultIcon(new ImageIcon("./resources/images/menuItems/Connect.png"));
			connectToServer.setHoveredIcon(new ImageIcon("./resources/images/menuItems/ConnectHover.png"));
			connectToServer.setClickedIcon(connectToServer.getIcon(PhalanxIconButton.DEFAULT_ICON));
		}
	}

	public PhalanxWindow getMasterFrame(){
		return this.masterFrame;
	}
	
	public PhalanxOnlineStatusPanel getStatusPanel(){
		return (PhalanxOnlineStatusPanel)statusFrame;
	}
}
